//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class CustomerRoleDBManager
    {
        #region Methods
        public static CustomerRoleCollection GetCustomerRolesByCustomerID(int CustomerID, bool showHidden)
        {
            CustomerRoleCollection customerRoleCollection = new CustomerRoleCollection();
            if (CustomerID == 0)
                return customerRoleCollection;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRoleLoadByCustomerID");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    CustomerRole customerRole = new CustomerRole();
                    customerRole.CustomerRoleID = NopDataHelper.GetInt(dataReader, "CustomerRoleID");
                    customerRole.Name = NopDataHelper.GetString(dataReader, "Name");
                    customerRole.FreeShipping = NopDataHelper.GetBoolean(dataReader, "FreeShipping");
                    customerRole.TaxExempt = NopDataHelper.GetBoolean(dataReader, "TaxExempt");
                    customerRole.Active = NopDataHelper.GetBoolean(dataReader, "Active");
                    customerRole.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    customerRoleCollection.Add(customerRole);
                }
            }

            return customerRoleCollection;
        }
        
        public static CustomerRoleCollection GetAllCustomerRoles(bool showHidden)
        {
            CustomerRoleCollection customerRoleCollection = new CustomerRoleCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRoleLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    CustomerRole customerRole = new CustomerRole();
                    customerRole.CustomerRoleID = NopDataHelper.GetInt(dataReader, "CustomerRoleID");
                    customerRole.Name = NopDataHelper.GetString(dataReader, "Name");
                    customerRole.FreeShipping = NopDataHelper.GetBoolean(dataReader, "FreeShipping");
                    customerRole.TaxExempt = NopDataHelper.GetBoolean(dataReader, "TaxExempt");
                    customerRole.Active = NopDataHelper.GetBoolean(dataReader, "Active");
                    customerRole.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    customerRoleCollection.Add(customerRole);
                }
            }

            return customerRoleCollection;
        }

        public static CustomerRole GetByCustomerRoleID(int CustomerRoleID)
        {
            CustomerRole customerRole = null;
            if (CustomerRoleID == 0)
                return customerRole;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRoleLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    customerRole = new CustomerRole();
                    customerRole.CustomerRoleID = NopDataHelper.GetInt(dataReader, "CustomerRoleID");
                    customerRole.Name = NopDataHelper.GetString(dataReader, "Name");
                    customerRole.FreeShipping = NopDataHelper.GetBoolean(dataReader, "FreeShipping");
                    customerRole.TaxExempt = NopDataHelper.GetBoolean(dataReader, "TaxExempt");
                    customerRole.Active = NopDataHelper.GetBoolean(dataReader, "Active");
                    customerRole.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                }
            }
            return customerRole;
        }

        public static CustomerRole InsertCustomerRole(string Name, bool FreeShipping, bool TaxExempt, bool Active, bool Deleted)
        {
            CustomerRole customerRole = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRoleInsert");
            db.AddOutParameter(dbCommand, "CustomerRoleID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "FreeShipping", DbType.Boolean, FreeShipping);
            db.AddInParameter(dbCommand, "TaxExempt", DbType.Boolean, TaxExempt);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int CustomerRoleID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@CustomerRoleID"));
                customerRole = GetByCustomerRoleID(CustomerRoleID);
            }
            return customerRole;
        }

        public static CustomerRole UpdateCustomerRole(int CustomerRoleID, string Name,
           bool FreeShipping, bool TaxExempt, bool Active, bool Deleted)
        {
            CustomerRole customerRole = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRoleUpdate");
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "FreeShipping", DbType.Boolean, FreeShipping);
            db.AddInParameter(dbCommand, "TaxExempt", DbType.Boolean, TaxExempt);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                customerRole = GetByCustomerRoleID(CustomerRoleID);

            return customerRole;
        }

        public static void AddCustomerToRole(int CustomerID, int CustomerRoleID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Customer_CustomerRole_MappingInsert");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static void RemoveCustomerFromRole(int CustomerID, int CustomerRoleID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Customer_CustomerRole_MappingDelete");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            db.ExecuteNonQuery(dbCommand);
        }
        #endregion
    }
}
